var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var openHelper = require("helper/open");
var communityUrl = require("url/community");
var communityUtil = require("helper/community");
var activityUtil = require("helper/activity");
var userUtil = require("helper/user");
var httpSilence = require("helper/httpSilence");
var qqHttp = require("helper/http");
var drawUrl = require("url/draw");
var userUtil = require("helper/user");
var root = ui("$");
var activityView = ui("activityView");
var circleView = ui("circleView");
var do_Button_join = ui("do_Button_join");

var bannerSlide = ui("bannerSlide");
var bannerView = ui("bannerSlide");
var bannerData = mm("do_ListData");
var albox0 = ui("banLabel01"),albox1 = ui("banLabel02"),albox2 = ui("banLabel03"),
	albox3 = ui("banLabel04"),albox4 = ui("banLabel05"),albox5 = ui("banLabel06");
var alboxcount =[albox0,albox1,albox2,albox3,albox4,albox5];
var isBanLoaded = false;
var rcount = 0;


var noticeSlide = ui("noticeSlide");
var isNewsLoaded = false;

do_Page.on("result", function(data) {
	if(communityUtil.getJoinCommunityState() == "1"){
		qqHttp.post(communityUrl.url.sign.detail,{},function(data){
			if(data.signCommunity==2){
				ui("do_Button_join").text="签到";
				ui("do_Button_join").bgColor="FE7F0BFF";
			}else{
				ui("do_Button_join").text="已签到";
				ui("do_Button_join").bgColor="949EAAFF";
			}
		});
	}
});

// 设置数据绑定的映射关系
root.setMapping({ // 此处的Mapping只能设置在 RootView 上 ;
	"do_Label_communityName.text" : "communityName",
	"do_Label_circle_count.text" : "circleCount",
	"do_Label_neighbor_count.text" : "neighborCount",
	"do_ALayout_49.visible":"newNeighborShow",
	"imgSignTip.visible":"newNeighborShow",
    "imgSignTip.text":"newNeighborCount",
	"do_Label_qShop_count.text" : "qShopCount",
	"noticeSlide.tag" : "noticeContent",
	"activityView.tag" : "activityList",
	"circleView.tag" : "circleBaseList",
	"bannerSlide.tag" : "crollbannerList",
	"do_Button_join.tag" : "curUserCommunityId"
});
var newsData = mm("do_ListData");
var activityData = mm("do_ListData");
var circleData = mm("do_ListData");
root.on("dataRefreshed", function(){
	if(!isBanLoaded){
		bannerData.removeAll();
		var dataBan = JSON.parse(bannerSlide.tag);
		rcount = dataBan.length>6?6:dataBan.length;
		for(var i = 0; i<6; i++){
			if(i<rcount){
				alboxcount[i].visible = true;
			}else{
				alboxcount[i].visible = false;
			}
		}
		bannerView.bindItems(bannerData);
		bannerData.addData(dataBan);
		bannerView.refreshItems();
		isBanLoaded = true;
	}
	/*公告*/
	if(!isNewsLoaded){
		var dataNews = JSON.parse(noticeSlide.tag);
		newsData.removeAll();
		noticeSlide.bindItems(newsData);
		newsData.addData(dataNews);
		noticeSlide.refreshItems();
		isNewsLoaded = true;
		noticeSlide.startLoop(5000);
		
		/*活动推荐*/
		activityData.removeAll();
		var dataActivity = JSON.parse(activityView.tag);
		activityView.bindItems(activityData);
		activityData.addData(dataActivity);
		activityView.refreshItems();
		/*圈子推荐*/
		circleData.removeAll();
		var dataActivity = JSON.parse(circleView.tag);
		circleView.bindItems(circleData);
		circleData.addData(dataActivity);
		circleView.refreshItems();
	}
});
bannerView.startLoop(3000);
//index点击社区
do_Page.on("community_update_isNewsLoaded", function(data) {
	isNewsLoaded=false;
	isBanLoaded=false;
});
// 公告更多
ui("do_ALayout_more_notice").on("touch","",300, function(data) {
	openHelper.open("source://view/community/notice/main.ui");
});
noticeSlide.on("touch","",300, function(data){
	var dataNews = JSON.parse(noticeSlide.tag);
	openHelper.open("source://view/community/notice/detail/main.ui",{id:dataNews[data.index].id});
});
// 活动更多
ui("do_ALayout_more_activity").on("touch","",300, function(data) {
	openHelper.open("source://view/activity/list/main.ui");
});
//推荐圈子更多
ui("do_ALayout_more_circle").on("touch","",300, function(data) {
	openHelper.open("source://view/community/circle/listAll.ui");
});
//圈子更多
ui("do_ALayout_more_circle_1").on("touch","",300, function(data) {
	openHelper.open("source://view/community/circle/list.ui");
});
//邻居更多
ui("do_ALayout_more_neighbor").on("touch","",300, function(data) {
	if(userUtil.loginToken()==="undefined"||userUtil.loginToken()===""){
		nf.toast("请先登录再操作");
		openHelper.openLogin();
		return;
	}
	ui("imgSignTip").visible = false;
	ui("do_ALayout_49").visible = false;
	if(communityUtil.communityVisitId()==ui("do_Button_join").tag){//当前社区用户才能查看邻里		
		openHelper.open("source://view/community/neighbor/newNeighborList.ui");
	}else{
		userUtil.saveUserPara(userUtil.userId(),'lastAccessDate-'+communityUtil.communityVisitId(),parseInt(new Date().getTime()/1000).toString());
		nf.toast("加入该社区，才能查看邻居。")
	}
});
//社区店铺
ui("do_ALayout_more_qShop").on("touch", "", 300, function(data) {
	openHelper.open("source://view/qshop/comShop/main.ui");
});
//社区详情
ui("do_ALayout_detail").on("touch", "", 300, function(data) {
	openHelper.open("source://view/community/detail/index.ui",{communityId:communityUtil.communityVisitId()});
});
if(communityUtil.getJoinCommunityState() == "1")
	{
	qqHttp.post(communityUrl.url.sign.detail,{},function(data){
		
		if(data.signCommunity==2){
			ui("do_Button_join").text="签到";
			ui("do_Button_join").bgColor="FE7F0BFF";

		}else{
			ui("do_Button_join").text="已签到";
			ui("do_Button_join").bgColor="949EAAFF";
		}
	});
	}
ui("do_Button_join").on("touch", "", 300,function(data) {
	if(communityUtil.getJoinCommunityState() == "1" )
		{
		openHelper.open("source://view/honor/index/index.ui");
		}
	else{
	nf.confirm("即将加入"+ui("do_Label_communityName").text+"，请确认是你所在社区。加入后一个月后才能退出切换到其它社区。", "", "确定", "取消", function(data, e) {
		if (data == 1) {
			joinCommunity();
		}
	})
	}
});
// 加入社区
function joinCommunity() {
	var params = {
		communityId : communityUtil.communityVisitId()
	};
	var httpAddJoinUrl = communityUrl.url.join.create;
	httpSilence.post(httpAddJoinUrl, params, function(data) {
		if (null != data.joinMessage) {
			nf.toast(data.joinMessage);
		}
		if (data.joinCount > 0) {// 加入成功
			do_Page.fire("index_community_tab_touch",{"brush":true});
			qqHttp.post(communityUrl.url.sign.detail,{},function(data){
				
				if(data.signCommunity==2){
					ui("do_Button_join").text="签到";
					ui("do_Button_join").bgColor="FE7F0BFF";

				}else{
					ui("do_Button_join").text="已签到";
					ui("do_Button_join").bgColor="949EAAFF";
				}
			});
		}
	})
}

/*轮播*/
bannerView.on("indexChanged", function(data, e) {
	var index = data;
	for(var i = 0; i<rcount; i++){
		if(data == i){
			alboxcount[i].bgColor = "ff831fAA";
		}else{
			alboxcount[i].bgColor = "FFFFFFAA";
		}
	}
	//nf.toast(data+":"+i);
});

do_Page.on("refreshSignState",function(data){
		ui("imgSignTip").visible = data.show;
		ui("do_ALayout_49").visible = data.show;
		if(parseInt(data.text)>9999)
			{
			 data.text = "999+";
			}
		ui("imgSignTip").text = data.text;
 
})
do_Page.on("refreshButton",function(data){
	if(communityUtil.getJoinCommunityState() == "1")
	{
		qqHttp.post(communityUrl.url.sign.detail,{},function(data){
			
			if(data.signCommunity==2){
				ui("do_Button_join").text="签到";
				ui("do_Button_join").bgColor="FE7F0BFF";

			}else{
				ui("do_Button_join").text="已签到";
				ui("do_Button_join").bgColor="949EAAFF";
			}
		});
	
	}
	else {
		ui("do_Button_join").text="加入社区";
		ui("do_Button_join").bgColor="FE7F0BFF";
	}
 
})